/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tag_analysis.client.applications;

import elaborate.tag_analysis.client.ServiceContext;
import elaborate.tag_analysis.client.event.TagProcessingEvent;
import elaborate.tag_analysis.client.event.TagProcessingEventListener;
import elaborate.tag_analysis.client.event.TagProcessingEventType;
import elaborate.tag_analysis.client.ui.model.OndemandResourceTreeModel;
import elaborate.tag_analysis.client.ui.model.ResourceTreeNode;
import elaborate.tag_analysis.model.Resource;
import elaborate.task.Task;
import elaborate.task.TaskQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingUtilities;

/**
 *
 * @author lendle
 */
public class DefaultApplicationContextImpl implements ApplicationContext {

    private List<TagProcessingEventListener> tagProcessingEventListeners = new ArrayList<TagProcessingEventListener>();
    private TaskQueue taskQueue = null;
    protected ServiceContext serviceContext;

    /**
     * Get the value of serviceContext
     *
     * @return the value of serviceContext
     */
    public ServiceContext getServiceContext() {
        return serviceContext;
    }

    /**
     * Set the value of serviceContext
     *
     * @param serviceContext new value of serviceContext
     */
    public void setServiceContext(ServiceContext serviceContext) {
        this.serviceContext = serviceContext;
    }

    public TaskQueue getTaskQueue() {
        return taskQueue;
    }

    public void setTaskQueue(TaskQueue taskQueue) {
        this.taskQueue = taskQueue;
    }

    public DefaultApplicationContextImpl(TaskQueue taskQueue) {
        this.taskQueue = taskQueue;
    }

    @Override
    public void fireTagProcessingEvent(TagProcessingEventType eventType, TagProcessingEvent event) {
        for (TagProcessingEventListener l : this.tagProcessingEventListeners) {
            if (eventType.equals(TagProcessingEventType.TAG_EXTRACTED)) {
                l.tagExtracted(event);
            }
        }
    }

    @Override
    public void executeTask(Task task) {
        this.taskQueue.addTask(task);
    }

    @Override
    public void addTagProcessingEventListener(TagProcessingEventListener l) {
        this.tagProcessingEventListeners.add(l);
    }

    @Override
    public void removeTagProcessingEventListener(TagProcessingEventListener l) {
        this.tagProcessingEventListeners.remove(l);
    }

    public void reloadResources() throws Exception {
        List<String> categories = this.serviceContext.getActiveApplication().getResourceHandler().getCategories();
        final OndemandResourceTreeModel treeModel = new OndemandResourceTreeModel();
        treeModel.setServiceContext(serviceContext);
        serviceContext.getResourceTree().setModel(treeModel);
        treeModel.getRootNode().setChildrenLoaded(true);
        treeModel.getRootNode().setAlt("Root");
        for (String category : categories) {
            Resource resource = new Resource();
            resource.setApplicationId(serviceContext.getActiveApplication().getApplicationId());
            resource.setId(category);
            resource.setName(category);
            resource.setValue(category);

            ResourceTreeNode resourceTreeNode = new ResourceTreeNode(resource);
            resourceTreeNode.setCategory(true);

            treeModel.getRootNode().getChildren().add(resourceTreeNode);
        }
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                serviceContext.getResourceTree().updateUI();
            }
        });
    }

    @Override
    public void executeTask(Task task, boolean preempt) {
        this.taskQueue.addTask(task, preempt);
    }
}
